package org.will.common;

import org.reveno.atp.api.ChannelOptions;
import org.reveno.atp.api.Configuration;
import org.reveno.atp.api.Reveno;
import org.reveno.atp.core.Engine;
import org.reveno.atp.core.RevenoConfiguration;
import org.reveno.atp.core.storage.FileSystemStorage;

import java.io.File;

/**
 * Created by EdifierWill on 2016/8/9.
 */
public class RevenoHelper {

    private static RevenoHelper _instance;

    private Reveno _reveno;

    public static RevenoHelper getInstance() {
        if (_instance == null) {
            synchronized (RevenoHelper.class) {
                if (_instance == null) {
                    _instance = new RevenoHelper();
                }
            }
        }
        return _instance;
    }

    private RevenoHelper()
    {
//        _reveno = new Engine("/tmp/incremental-sample");
//        //_reveno.config().cpuConsumption(Configuration.CpuConsumption.LOW);
//        _reveno.config().journaling().minVolumes(1);
//        _reveno.config().journaling().volumes(4);
//        _reveno.config().journaling().volumesSize(0, 1024);
//        _reveno.config().journaling().channelOptions(ChannelOptions.BUFFERING_MMAP_OS);
//        _reveno.config().disruptor().bufferSize(1024 * 4);
//        _reveno.config().snapshotting().every(10_000);
//        _reveno.config().snapshotting().atShutdown(true);

        File dirFile = new File("/tmp/incremental-sample");
        RevenoConfiguration.RevenoJournalingConfiguration config = new RevenoConfiguration().revenoJournaling();
        FileSystemStorage storage = new FileSystemStorage(dirFile,config);
        ExtSnapshotStorage extSnapshotStorage =new ExtSnapshotStorage(dirFile,config);
        _reveno = new Engine(storage,storage, extSnapshotStorage,RevenoHelper.class.getClassLoader());

        _reveno.config().snapshotting().every(200_000);
        _reveno.config().snapshotting().atShutdown(true);
    }

    public Reveno getReveno()
    {
        return _reveno;
    }
}
